package com.hightrast.service.config;

import cn.dev33.satoken.exception.SaTokenException;
import com.hightrast.service.api.baseEntity.R;
import com.hightrast.service.api.baseEntity.ResultCode;
import com.hightrast.service.api.baseEntity.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.servlet.http.HttpServletRequest;

/**
 * 全局异常处理
 */
@Slf4j
@RestControllerAdvice
@ResponseBody
public class RestExceptionHandler {

    /**
     * saToken异常
     *
     * @param e SaTokenException
     * @return
     */
    @ExceptionHandler(SaTokenException.class)
    public R<String> saTokenException(SaTokenException e) {
        log.error("saToken异常 code={}, SaTokenException = {}", e.getCode(), e.getMessage(), e);
        return R.fail(e.getCode(), e.getMessage());
    }


    /**
     * 处理自定义异常
     *
     * @param e ServiceException
     * @return
     */
    @ExceptionHandler(ServiceException.class)
    public R<String> serviceException(ServiceException e) {
        log.error("业务异常 code={}, ServiceException = {}", e.getResultCode().getCode(), e.getMessage(), e);
        return R.fail(e.getResultCode().getCode(), e.getMessage());
    }

    /**
     * 处理空指针的异常
     *
     * @param e NullPointerException
     * @return
     * @description 空指针异常定义为前端传参错误，返回400
     */
    @ExceptionHandler(value = NullPointerException.class)
    public R<String> nullPointerException(NullPointerException e) {
        log.error("空指针异常 NullPointerException ", e);
        return R.fail(ResultCode.NULL_POINTER.getCode(), ResultCode.NULL_POINTER.getMessage());
    }

    /**
     * 处理404异常
     *
     * @param e NoHandlerFoundException
     * @return
     */
    @ExceptionHandler(NoHandlerFoundException.class)
    public R<String> noHandlerFoundException(HttpServletRequest req, Exception e) {
        log.error("404异常 NoHandlerFoundException, method = {}, path = {} ", req.getMethod(), req.getServletPath(), e);
        return R.fail(ResultCode.NOT_FOUND.getCode(), ResultCode.NOT_FOUND.getMessage());
    }

    /**
     * 处理请求方式错误(405)异常
     *
     * @param e HttpRequestMethodNotSupportedException
     * @return
     */
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public R<String> HttpRequestMethodNotSupportedException(HttpServletRequest req, Exception e) {
        log.error("请求方式错误(405)异常 HttpRequestMethodNotSupportedException, method = {}, path = {}", req.getMethod(), req.getServletPath(), e);
        return R.fail(ResultCode.METHOD_NOT_SUPPORTED.getCode(), ResultCode.METHOD_NOT_SUPPORTED.getMessage());
    }

    /**
     * 处理其他异常
     *
     * @param e otherException
     * @return
     */
    @ExceptionHandler(Exception.class)
    public R<String> exception(Exception e) {
        log.error("未知异常 exception = {}", e.getMessage(), e);
        return R.fail(ResultCode.INTERNAL_SERVER_ERROR.getCode(), e.getMessage());
    }
}